package com.zxl.live.screen.screen.ui.widget;

import android.content.Context;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.zxl.live.screen.screen.ui.widget.surface.FrameController;
import com.zxl.live.screen.screen.utils.ScreenUtils;

/**
 * Created by Administrator on 2017/11/8.
 */

public class ScreenSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    public static final long REFFRESH_TIME = 30;

    private boolean mIsPause = false;

    private boolean mIsStart = false;

    private Paint mPaint;

    private SurfaceHolder mHolder;

    private Camera mCamera = new Camera();

    private Matrix mMatrix = new Matrix();

    private FrameController mController;

    public ScreenSurfaceView(Context context) {
        super(context);
        setFocusable(true);
        setZOrderOnTop(true);
        mHolder = getHolder();
        mHolder.setFormat(PixelFormat.TRANSPARENT);
        mHolder.addCallback(this);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
        mController = FrameController.getInstance();
        mController.setScreenSurfaceView(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        stop();
    }

    public void start() {
        if (!mIsStart) {
            mIsStart = true;
            new Thread(this).start();
            new Thread(mController).start();
            mController.onStart();
        }
    }

    public void stop() {
        if (mIsStart) {
            mIsStart = false;
            mController.onStop();
        }
    }

    public void pause() {
        if (!mIsPause) {
            mIsPause = true;
            mController.onPause();
        }
    }

    public void resume() {
        if (mIsPause) {
            mIsPause = false;
            mController.onResume();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mController.onTouchEvent(event);
    }

    @Override
    public void run() {
        while (!Thread.interrupted() && mIsStart) {
            long start = System.currentTimeMillis();
            if (!mIsPause) {
                Canvas canvas = null;
                try {
                    synchronized (mHolder) {
                        canvas = mHolder.lockCanvas();
                        int save = canvas.save();
                        canvas.scale(ScreenUtils.sScaleX, ScreenUtils.sScaleY, ScreenUtils.sWidthPixels / 2, ScreenUtils.sHeightPixels / 2);
                        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
                        mController.doDraw(mCamera, mMatrix, canvas, mPaint);
                        canvas.restoreToCount(save);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (null != canvas) {
                        try {
                            mHolder.unlockCanvasAndPost(canvas);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            long sub = System.currentTimeMillis() - start;
            if (sub < REFFRESH_TIME) {
                try {
                    Thread.sleep(ScreenSurfaceView.REFFRESH_TIME - sub);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public boolean isPause() {
        return mIsPause;
    }

    public boolean isStart() {
        return mIsStart;
    }
}
